﻿using AutoMapper;
using Blog.Core.Domain.Model.User;
using Blog.Core.DTO;
using Blog.Core.DTO.ViewModel;
using Blog.Core.DTO.ViewModel.User;
using Blog.Core.IRepositories;
using Blog.Core.IRepositories.IUser;
using Blog.Core.IServices.IUser;
using Blog.Core.Repositories;
using Blog.Core.Repositories.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Blog.Core.Services.User
{
	/// <summary>
	/// 部门信息服务
	/// </summary>
	public class DepartmentInfoService : BaseService<DepartmentInfo>, IDepartmentInfoService
	{
		private readonly IDepartmentInfoRepository repository;
		private readonly IDepartmentUserRepository userrepository;
		private readonly IUserInfoRepository userinfor;
		private readonly IUnOfWork unOfWork;
		private readonly IMapper mapper;
		public DepartmentInfoService(IDepartmentInfoRepository repository, IUnOfWork unOfWork, IMapper mapper, IDepartmentUserRepository userrepository, IUserInfoRepository userinfor) : base(repository, unOfWork)
		{
			this.repository = repository;
			this.userrepository = userrepository;
			this.unOfWork = unOfWork;
			this.mapper = mapper;
			this.userinfor = userinfor;
		}
		/// <summary>
		/// 树形显示
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<List<DepartmentInfoDTO>> DepartmentList(int id)
		{
			List<DepartmentInfoDTO> result = new List<DepartmentInfoDTO>();
			var list = await repository.GetAll();
			var entity = list.ToList().Where(x => x.Pid == id);
			foreach (var m in entity)
			{
				result.Add(new DepartmentInfoDTO
				{
					Id = m.Id,
					Pid = m.Pid,
					Name = m.Name,
					children = await DepartmentList(m.Id),
				});
			}
			return result;
		}
		/// <summary>
		/// 部门用户关系
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<List<UserInfo>> DepartmentUser(int? id)
		{
			List<UserInfo> result = new List<UserInfo>();
			var user = await userinfor.GetAll();
			if (id != null && id != 1)
			{
				var list = await userrepository.GetAll();
				var list1 = list.Where(x => x.DepartmentId == id).Select(x => x.UserId).ToList();
				foreach (var m in list1)
				{
					var depart = user.Where(y => y.Id == m).FirstOrDefault();
					result.Add(depart);
				}
			}
			else
			{
				result = user.ToList();
			}
			return result;
		}
		/// <summary>
		/// 部门添加
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		public async Task<ResponseResultDTO<int>> DepartmentInfoAdd(DepartmentInfo dto)
		{
			var list = await repository.GetAll();
			list = list.Where(m => m.Name.Equals(dto.Name)).ToList();
			if (list.Count() > 0)
			{
				return new ResponseResultDTO<int>
				{
					Code = ResultStatusCode.HANDLEFAIL,
					Data = list.Count(),
					Message = "部门已存在！",//部门已存在！
				};
			}
			await repository.Add(dto);
			unOfWork.SaveChanges();
			return new ResponseResultDTO<int>
			{
				Code = ResultStatusCode.SUCCESS,
				Message = "部门添加成功！",//部门添加成功！
			};
		}
	}
}
